package com.szqhyxj.youxiangju.biz.partnerHome.home.peasonalperformancerepayment;


import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/21.
 */

public class PeasonalPerformanceRepaymentPresenter implements PeasonalPerformanceRepaymentContract.Presenter {
    PeasonalPerformanceRepaymentContract.View view;
    List<PeasonalRowsItem> list;
    private final static int ORGANTYPE = 1;
    private final static int STATISTIVSTYPE = 1;
    private final static int PAGESIZE = 10;
    private int PAGENUMBER = 1;
    private int NUM;

    public PeasonalPerformanceRepaymentPresenter(PeasonalPerformanceRepaymentContract.View view) {
        this.view = view;
        list = new ArrayList<>();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init(int organType, int statisticsType, int pageSize, int pageNumber) {
        PeasonalPerformanceRepaymentModel.getpartnerEquipmentPayment(ORGANTYPE, STATISTIVSTYPE, PAGESIZE, PAGENUMBER)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity>>() {
                    @Override
                    public void accept(BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity> peasonalPerformanceRepaymentEntityBaseResult) throws Exception {
                        if (peasonalPerformanceRepaymentEntityBaseResult.businessLogicSuccess()) {
                            NUM = peasonalPerformanceRepaymentEntityBaseResult.getData().getTotal() / 10 + 1;

                            list.addAll(peasonalPerformanceRepaymentEntityBaseResult.getData().getRows());
                            view.showData(list);

                        } else {
                            view.showToast(peasonalPerformanceRepaymentEntityBaseResult.getData() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });
    }

    @Override
    public void load() {
        if (PAGENUMBER < NUM) {
            PAGENUMBER = ++PAGENUMBER;
            PeasonalPerformanceRepaymentModel.getpartnerEquipmentPayment(ORGANTYPE, STATISTIVSTYPE, PAGESIZE, PAGENUMBER)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity>>() {
                        @Override
                        public void accept(BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity> teamPerformanceRepaymentEntityBaseResult) throws Exception {
                            if (teamPerformanceRepaymentEntityBaseResult.businessLogicSuccess()) {

                                List<PeasonalRowsItem> list = new ArrayList<>();
                                list.addAll(teamPerformanceRepaymentEntityBaseResult.getData().getRows());
                                view.showLoad(list);

                            } else {
                                view.showToast(teamPerformanceRepaymentEntityBaseResult.getData() + "");
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            view.showToast(throwable.getMessage() + "");
                        }
                    });
        } else {
            view.showNodata();
            view.showToast("最后一页");
        }

    }

    @Override
    public void search(String name) {
        PeasonalPerformanceRepaymentModel.getonepartnerEquipmentPayment(name)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity>>() {
                    @Override
                    public void accept(BaseResult<PeasonalPerformanceRepaymentModel.PeasonalPerformanceRepaymentEntity> teamPerformanceRepaymentEntityBaseResult) throws Exception {
                        if (teamPerformanceRepaymentEntityBaseResult.businessLogicSuccess()) {
                            List<PeasonalRowsItem> list = new ArrayList<>();
                            view.showData(list);
                        } else {
                            view.showToast(teamPerformanceRepaymentEntityBaseResult.getData() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }
}
